package com.chw.utils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.chw.constants.Constants;

/**
 * 功能描述: 公用字段设置工具类
 */
public class CommonFieldsUtil {

	/** 日志记录 */
	private static LogUtil log = LogUtil.newInstance(CommonFieldsUtil.class);

	/**
	 * 私有构造方法
	 */
	private CommonFieldsUtil() {
	}

	public static String getCurrentUserId() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		if (request != null) {
			AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
			if (principal != null) {
				Map<String, Object> attributes = principal.getAttributes();
				return (String) attributes.get("id");

			} else {
				return "4249797283594fe0a03d6deec8d220ba";
			}
		}
		return null;
	}

	/**
	 * 设置对象公用字段属性的值 => 操作为:创建
	 * 
	 * @param object
	 */
	public static void setCreateValue(Object object) {
		Class<? extends Object> clazz = object.getClass();
		// 获取Session用户ID(userId) -- 创建人
		String userId = getCurrentUserId();
		// get userId from Session
		// default userId = 1
		/*
		 * Object user = session.getAttribute(SessionKeys.SESSION_INFO_USERID);
		 */
		// Session中的未取到用户Id 说明未进行登录 则设置创建者用户Id为1
		setCreateValue(object, userId);
	}

	public static void setCreateValueByCy(Object object) {
		String userId = getCurrentUserId();
		setCreateValueByCy(object, userId);
	}

	/**
	 * 设置对象公用字段属性的值 => 操作为:更新
	 * 
	 * @param object
	 */
	public static void setUpdateValue(Object object) {
		// Class<? extends Object> clazz = object.getClass();
		// 获取Session用户ID(userId) -- 更新人
		String userId = getCurrentUserId();
		// get userId from Session
		// default userId = 1

		// 更新时间
		setCommonFieldsUpdateValue(object, userId, new Date());
	}

	/**
	 * 设置对象公用字段属性的值 => 操作为:创建
	 * 
	 * @param object
	 * @param userId
	 */
	public static void setCreateValue(Object object, String userId) {
		// 创建对象时的创建时间和最后更新时间相同
		// 创建对象时的创建人和最后更新人相同
		Date date = new Date();
		setCommonFieldsValue(object, userId, date, userId, date);
		// 设置创建的UUID
		setCommonFieldsUUID(object, UUIDUtil.getUUID());
	}

	public static void setCreateValueByCy(Object object, String userId) {
		// 创建对象时的创建时间和最后更新时间相同
		// 创建对象时的创建人和最后更新人相同
		Date date = new Date();
		setCommonFieldsValue(object, userId, date, userId, date);
		/*
		 * // 设置创建的UUID setCommonFieldsUUID(object, UUIDUtil.getUUID());
		 */
	}

	/**
	 * 设置对象公用字段属性的值 => 操作为:更新
	 * 
	 * @param object
	 * @param userId
	 */
	public static void setUpdateValue(Object object, String userId) {
		setCommonFieldsUpdateValue(object, userId, new Date());
	}

	/**
	 * 设置对象公用字段属性的值 -- 操作 -> 设置公用字段值
	 * 
	 * @param object
	 * @param operatorBy
	 *            操作人
	 */
	public static void setCommonFieldsValue(Object object, String operatorBy) {
		Class<? extends Object> clazz = object.getClass();
		try {
			// 无UUID 则为新建
			Method getId = clazz.getDeclaredMethod("getId");
			Object uuid = getId.invoke(object);
			if (uuid == null) {
				// 新增
				setCreateValue(object, operatorBy);
			} else {
				// 修改
				setUpdateValue(object, operatorBy);
			}
		} catch (Exception ex) {
			log.error(ex);
			ex.printStackTrace();
		}
	}

	/**
	 * 设置对象公用字段属性的值 => 操作为:创建
	 * 
	 * @param object
	 * @param createdBy
	 * @param createdDate
	 * @param lastUpdatedBy
	 * @param lastUpdatedDate
	 */
	private static void setCommonFieldsValue(Object object, String createdBy, Date createdDate, String lastUpdatedBy,
			Date lastUpdatedDate) {
		setCommonFieldsCreateValue(object, createdBy, createdDate);
		setCommonFieldsUpdateValue(object, lastUpdatedBy, lastUpdatedDate);
		setCommonFieldsFlagValue(object);
	}

	/**
	 * 设置对象公用字段属性的值 -- 创建操作 -> 设置公用字段值
	 * 
	 * @param object
	 * @param createdBy
	 * @param createdDate
	 */
	private static void setCommonFieldsCreateValue(Object object, String createdBy, Date createdDate) {
		Class<? extends Object> clazz = object.getClass();
		try {
			// 设置创建人
			Method setCreatedBy = clazz.getDeclaredMethod("setCreatedBy", String.class);
			setCreatedBy.invoke(object, createdBy);
			// 设置创建时间
			Method setCreatedDate = clazz.getDeclaredMethod("setCreatedDate", Date.class);
			setCreatedDate.invoke(object, createdDate);
		} catch (Exception ex) {
			log.error(ex);
			ex.printStackTrace();
		}
	}

	/**
	 * 设置对象公用字段属性的值 -- 更新操作 -> 设置公用字段值
	 * 
	 * @param object
	 * @param lastUpdatedBy
	 * @param lastUpdatedDate
	 */
	private static void setCommonFieldsUpdateValue(Object object, String lastUpdatedBy, Date lastUpdatedDate) {
		Class<? extends Object> clazz = object.getClass();
		try {
			// 设置最后更新人
			Method setLastUpdatedBy = clazz.getDeclaredMethod("setLastUpdatedBy", String.class);
			setLastUpdatedBy.invoke(object, lastUpdatedBy);
			// 设置最后更新时间
			Method setLastUpdatedDate = clazz.getDeclaredMethod("setLastUpdatedDate", Date.class);
			setLastUpdatedDate.invoke(object, lastUpdatedDate);
		} catch (Exception ex) {
			log.error(ex);
			ex.printStackTrace();
		}
	}

	/**
	 * 设置对象公用字段属性的值 -- 状态设置
	 * 
	 * @param object
	 */
	private static void setCommonFieldsFlagValue(Object object) {
		Class<? extends Object> clazz = object.getClass();
		try {
			// 设置逻辑删除标识 默认为不删除
			Method setEnabledFlag = clazz.getDeclaredMethod("setEnabledFlag", String.class);
			setEnabledFlag.invoke(object, Constants.ENABLED_FLAG_Y);
			// 设置禁用/启用标识 默认为启用
			Method setStatus = clazz.getDeclaredMethod("setStatus", String.class);
			setStatus.invoke(object, Constants.STATUS_FLAG_Y);
		} catch (Exception ex) {
			log.error(ex);
			ex.printStackTrace();
		}
	}

	/**
	 * 设置对象公用字段属性的值 -- 创建操作 -> 设置公用字段值 UUID
	 * 
	 * @param object
	 */
	private static void setCommonFieldsUUID(Object object, String uuid) {
		Class<? extends Object> clazz = object.getClass();
		try {
			// 设置创建的主键ID
			Method setId = clazz.getDeclaredMethod("setId", String.class);
			setId.invoke(object, uuid);
		} catch (Exception ex) {
			log.error(ex);
			ex.printStackTrace();
		}
	}

}
